home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 220 / 220.xpi / chrome / flashgot.jar / content / flashgot / flashgotDMOverlay.js < prev    next >
Encoding:
JavaScript  |  2010-01-24  |  9.0 KB  |  303 lines

  1. /***** BEGIN LICENSE BLOCK *****
  2.  
  3.     FlashGot - a Firefox extension for external download managers integration
  4.     Copyright (C) 2004-2009 Giorgio Maone - g.maone@informaction.com
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.                              
  20. ***** END LICENSE BLOCK *****/
  21.  
  22. var gFlashGotDMDialog = null;
  23.  
  24. function FlashGotDMDialog() {
  25.   gFlashGotDMDialog = this;
  26.  
  27.   this.url = dialog.mLauncher.source.spec;
  28.   
  29.   
  30.   
  31.   try {
  32.     this.openerDocument = dialog.mContext.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
  33.       .getInterface(Components.interfaces.nsIDOMWindow).document;
  34.   } catch(ex) {
  35.     this.openerDocument = top.opener && top.opener.content && top.opener.content.document || null;
  36.   }
  37.   
  38.   try {
  39.       this.referrer = dialog.mContext.QueryInterface(
  40.         Components.interfaces.nsIWebNavigation).currentURI.spec;
  41.   } catch(ex) {
  42.      this.referrer = this.openerDocument && this.openerDocument.URL || this.url;
  43.   }
  44.   
  45.   
  46.   this.dialog = dialog;
  47.   
  48.  
  49.   this.fname = dialog.mLauncher.suggestedFileName;
  50.   var ext=this.fname.split('.');
  51.   this.ext = ext.length > 0 ? ext[ext.length -1].toLowerCase() : "";
  52.   this.extensionExists = gFlashGotService.extensions.indexOf(this.ext) > -1;
  53.   const itc = gFlashGotService.interceptor;
  54.   
  55.   
  56.   if(itc.lastPost) {
  57.     gFlashGotService.log("Recent post info found: " + itc.lastPost + ", " +
  58.                          itc.lastPost.URI.spec + " VS " + this.url + ", " +
  59.                          itc.lastPost.isPending() +
  60.                          ", " + (itc.lastPost.URI == dialog.mLauncher.source));
  61.     if(itc.lastPost.URI.spec == this.url &&
  62.        (itc.lastPost.isPending() || itc.lastPost.URI == dialog.mLauncher.source)) {
  63.       this.postChannel = itc.lastPost;
  64.     }
  65.   }
  66.   
  67.   if(gFlashGotService.DMS.found && (!itc.bypassAutoStart)
  68.       && (itc.forceAutoStart
  69.           || ( itc.autoStart
  70.             && (itc.interceptAll
  71.                 || this.extensionExists)))) {
  72.     this.download();
  73.     return;
  74.   }
  75.  
  76.   window.setTimeout(function() { gFlashGotDMDialog.init(); }, 0);
  77.   
  78.   if(typeof(ReGetDmDialog) != "undefined") {
  79.     ReGetDmDialog.prototype.init = function() {};
  80.     document.getElementById("regetRadio").style.display = "none";
  81.     document.getElementById("regetBasic").style.display = "none";
  82.   }
  83.   
  84.   this.forceNormal();
  85. }
  86.  
  87. FlashGotDMDialog.prototype = {
  88.   get choosen() {
  89.     return gFlashGotService.getPref("dmchoice", false);
  90.   }, 
  91.   set choosen(v) {
  92.     gFlashGotService.setPref("dmchoice", v);
  93.     return v;
  94.   },
  95.   
  96.  
  97.   remember: null,
  98.   choice: null,
  99.   check: null,
  100.  
  101.   forceNormal: function(secondChance) {
  102.     var basicBox = document.getElementById('basicBox');
  103.     var normalBox = document.getElementById('normalBox');
  104.     var self = this;
  105.     if ((normalBox && basicBox)) {
  106.       if (normalBox.collapsed && basicBox.collapsed && !secondChance) {
  107.         window.setTimeout(function() { self.forceNormal(true); }, 10);
  108.         return;
  109.       }
  110.       if (normalBox.collapsed) {
  111.         
  112.         var e = document.getElementById('open');
  113.         e.parentNode.collapsed = true;
  114.         e.disabled = true;
  115.         
  116.         var nodes = normalBox.getElementsByTagName('separator');
  117.         for (var j = nodes.length; j-- > 0;) {
  118.           nodes[j].collapsed = true;
  119.         }
  120.         
  121.         basicBox.collapsed = true;
  122.         normalBox.collapsed = false;
  123.       }
  124.     }
  125.     self.sizeToContent();
  126.   },
  127.   
  128.   sizeToContent: function() {
  129.     try {
  130.       window.sizeToContent();
  131.     } catch(e) {
  132.       dump(e + "\n");
  133.       try {
  134.                 var btn = document.documentElement.getButton('accept');
  135.                 window.innerHeight = btn.boxObject.y + 10; 
  136.             }
  137.             catch (e) {
  138.                 dump(e + "\n");
  139.             }        
  140.     }
  141.     var boxes = document.getAnonymousNodes(document.documentElement);
  142.     var h = 48; // margin
  143.     var w = 0;
  144.     var bo;
  145.     for (var j = boxes.length; j-- > 0;) {
  146.       bo = boxes[j].boxObject;
  147.       h += bo.height;
  148.       w = Math.max(w, bo.width);
  149.     }
  150.     w += 48; // margin
  151.     dump("Heights: " + h + ", " + window.outerHeight + "\n");
  152.     h = Math.max(window.outerHeight, h);
  153.     w = Math.max(window.outerWidth, w);
  154.     window.resizeTo(window.outerWidth, h);
  155.     if (window.outerWidth < w || window.outerHeight < h) 
  156.       window.resizeTo(w, h);
  157.   },
  158.   
  159.   init: function() {
  160.     
  161.     const dmsMenu = this.dmsMenu = document.getElementById("flashgot-dms");
  162.       
  163.     this.remember = document.getElementById("rememberChoice") || document.getElementById("alwaysHandle");
  164.     if(this.remember) {
  165.       this.remember.collapsed = false;
  166.       if(this.remember.id == "rememberChoice" && 
  167.         this.remember.parentNode.previousSibling &&
  168.         this.remember.parentNode.previousSibling.nodeName == "separator") {
  169.         this.remember.parentNode.previousSibling.collapsed = false;
  170.       }
  171.     }
  172.    
  173.     
  174.     this.choice = document.getElementById("flashgot-dmradio");
  175.     this.check = document.getElementById("flashgot-dmcheck");
  176.  
  177.     const dms = gFlashGotService.DMS;
  178.     if(!dms.found) {
  179.       this.choice.setAttribute("disabled", "true");
  180.       if(this.check) this.check.setAttribute("disabled", "true");
  181.       dmsMenu.setAttribute("collapsed", "true");
  182.       return;
  183.     }
  184.     const defaultDM = gFlashGotService.defaultDM; 
  185.     
  186.     
  187.  
  188.     var menuItem;
  189.     var enabledDMSs=0;
  190.     dmsMenu.removeAllItems();
  191.     var dm;
  192.     for(var j=0, len = dms.length; j<len; j++) {
  193.       dm=dms[j];
  194.       if(dm.supported) {
  195.         enabledDMSs++;
  196.         menuItem=dmsMenu.appendItem(dm.name, dm.codeName);
  197.         if(defaultDM==dm.name) {
  198.           dmsMenu.selectedItem=menuItem;
  199.         }
  200.       }
  201.     }
  202.     
  203.     const modeRadioGroup=document.getElementById("mode");
  204.     
  205.     if(enabledDMSs < 2) {
  206.       dmsMenu.setAttribute("collapsed","true");
  207.     } else {
  208.       dmsMenu.addEventListener("popuphidden", function() {
  209.         gFlashGotDMDialog.toggleChoice();
  210.       }, true);
  211.       
  212.       const openRadio = document.getElementById("open");
  213.       if(openRadio) {
  214.         var maxWidth = Math.max(
  215.           openRadio.boxObject.width, this.choice.boxObject.width
  216.         );
  217.         if(maxWidth > 0) openRadio.width = this.choice.width = maxWidth;
  218.       }
  219.     }
  220.     
  221.     if(this.choosen) {
  222.       if(this.remember) this.remember.checked = this.extensionExists && gFlashGotService.interceptor.autoStart;
  223.       document.getElementById("mode").selectedItem = this.choice;
  224.       if(this.check) this.check.checked = true;
  225.     }
  226.     
  227.     this.toggleChoice();
  228.     modeRadioGroup.addEventListener(
  229.       "select", function(event) {
  230.         gFlashGotDMDialog.toggleChoice(event)
  231.       },true);
  232.     
  233.     var d = document.documentElement;
  234.     d.setAttribute('ondialogaccept',
  235.       'if(gFlashGotDMDialog.dialogAccepted()) { '
  236.       + document.documentElement.getAttribute('ondialogaccept')
  237.       +'}');
  238.       d.setAttribute("onblur", "if(dialog) {" + d.getAttribute("onblur") + " }");
  239.   }
  240. ,
  241.   toggleChoice: function() {
  242.     var dmchoice = document.getElementById("mode").selectedItem == this.choice;
  243.     
  244.     this.choosen = dmchoice;
  245.     var remember = this.remember;
  246.     
  247.     if(dmchoice) {
  248.       this.dmsMenu.removeAttribute("disabled");
  249.       window.setTimeout(
  250.         function() { 
  251.           document.documentElement.getButton('accept').disabled = false;
  252.         }, 10);
  253.       if(remember) {
  254.         remember.disabled = false;
  255.       }
  256.     } else {
  257.       this.dmsMenu.setAttribute("disabled", true);
  258.     }
  259.   }
  260. ,
  261.    dialogAccepted: function() {
  262.     if(this.choosen) {
  263.       if(this.remember && this.remember.checked) {
  264.         gFlashGotService.addExtension(this.ext);
  265.         gFlashGotService.interceptor.autoStart = true;
  266.       }
  267.       if(this.dmsMenu.selectedItem) {
  268.         gFlashGotService.defaultDM = this.dmsMenu.selectedItem.getAttribute("label");
  269.       }
  270.       this.download();
  271.       return false;
  272.     } else {
  273.       return true;
  274.     }
  275.   }
  276. ,
  277.   download: function() {
  278.     var links=[ {
  279.        href: this.url, 
  280.        description: this.fname,
  281.        noRedir: true
  282.     } ];
  283.     links.referrer = this.referrer;
  284.     links.document = this.openerDocument;
  285.     links.browserWindow = gFlashGotService.getBrowserWindow(links.document);
  286.     if(this.postChannel) {
  287.       gFlashGotService.interceptor.extractPostData(this.postChannel, links);
  288.     }
  289.     gFlashGotService.download(links);
  290.     with(document.documentElement) {
  291.       removeAttribute('ondialogaccept');
  292.       removeAttribute('onblur');
  293.       removeAttribute('onfocus');
  294.       cancelDialog();
  295.     }
  296.   }
  297. }
  298.  
  299. window.addEventListener("load",  function(e) { new FlashGotDMDialog(); }, false);
  300.  
  301.  
  302.  
  303.